<template>
  <div>
      <h1>登录</h1>
      <van-form>
        <van-field label="手机号" v-model="mobile" :rules="[{required:true, message:'手机号不能为空'}]" />
        <van-button @click="send" :disabled="disabled"> {{mes}} </van-button>
        <van-field label="验证码"  v-model="code"/>
        <van-button @click="add">登录</van-button>
      </van-form>
  </div>
</template>

<script>
import myaxios from 'axios'
export default {
    data(){
        return{
            mobile:'',
            code:'',
            mes:'发送验证码',
            time:60,
            disabled:false
        }
    },
    methods:{
      sendkm(){
        myaxios({url:'http://127.0.0.1:5000/duanxin?mobile='+this.mobile, method:'get'})
        .then(res=>{
          console.log(res.data)
          if(res.data.code == 200){
            return true
          }
          return false
        })
      },
      chagemes(){
        if(this.time == 0){
          this.mes = '发送验证码'
          this.disabled = false
        }else{
          this.mes = this.time
          this.time -= 1
          this.disabled = true
        }
      },
        send(){
          let rea = /^1[3-9]\d{9}$/
          if(!rea.test(this.mobile)){
            alert('请输入真确手机号')
            return false
          }
         myaxios({url:'http://127.0.0.1:5000/duanxin?mobile='+this.mobile, method:'get'})
          .then(res=>{
            console.log(res.data)
            if(res.data.code==200){
              this.time = 60
              setInterval(this.chagemes, 1000)
            }
          return false
          })
        },
        add(){
            let date = {'mobile': this.mobile, 'code': this.code}
            myaxios({url:'http://127.0.0.1:5000/login', method:'post', data:date}).then(res=>{
              console.log(res)
              localStorage.setItem('id', res.data.user.id)
              localStorage.setItem('mobile', res.data.user.name)
              localStorage.setItem('token', res.data.token)
              localStorage.setItem('retoken', res.data.retoken)
              let reslist = JSON.stringify(res.data.resources)
              localStorage.setItem('reslist', reslist)
            })
    }
  }

}
</script>

<style>

</style>